/**
 * Created by machan on 2017/5/24.
 */

class PickerUtils {

    // /**
    //  * 由于小程序的picker-view组件问题，不得不增加该方法对获取到的code值做校验
    //  * @param linkedArr
    //  * @param code
    //  */
    // isLinkedCodeFull(linkedArr, code) {
    //     if (!code) {
    //         return false;
    //     }
    //
    //     let codes = [];
    //     if (code.indexOf(",") >= 0) {
    //         codes = code.split(",");
    //     } else {
    //         codes = code.split("/");
    //     }
    //
    //     let parent = {};
    //     for (let i = 0; i <= codes.length; i++) {
    //         let helpers = {
    //             _filter: function (item) {
    //                 return item.code === codes[i];
    //             }
    //         };
    //
    //         if (i === 0) {
    //             parent = linkedArr.filter(helpers._filter)[0];
    //         } else if (i === codes.length) {
    //             //最后一个没有children为正确
    //             return parent ? !parent.children: false;
    //         } else {
    //             parent = parent ? parent.children.filter(helpers._filter)[0] : "";
    //         }
    //     }
    //     return !!parent;
    // }

    /**
     * linkedPicer通过code获取汉字数组
     * @param linkedArr
     * @param code code字符串
     *  example:
     *  1024，1024，1024
     *  1024/1024/1024
     * @returns {*}
     */
    getLinkedHZByCode(linkedArr, code) {
        if (!code) {
            return "";
        }

        let codes = [];
        if (code.indexOf(",") >= 0) {
            codes = code.split(",");
        } else {
            codes = code.split("/");
        }

        let hzArr = [];
        let parent = {};
        for (let i = 0; i < codes.length; i++) {
            let helpers = {
                _filter: function (item) {
                    return item.code === codes[i];
                }
            };

            if (i === 0) {
                parent = linkedArr.filter(helpers._filter)[0];
            } else {
                parent = parent ? parent.children.filter(helpers._filter)[0] : "";
            }
            parent && hzArr.push(parent.label);
        }

        return hzArr;
    }

    /**
     * linkedPicer通过汉字获取code数组
     * @param linkedArr
     * @param hz 汉字串
     *  example:
     *  北京，北京市，海淀区
     *  北京/北京市/海淀区
     * @returns {*}
     */
    getLinkedCodeByHZ (linkedArr, hz) {
        if (!hz) {
            return "";
        }

        let hzs = [];
        if (hz.indexOf(",") >= 0) {
            hzs = hz.split(",");
        } else {
            hzs = hz.split("/");
        }


        let codeArr = [];
        let parent = {};
        for (let i = 0; i < hzs.length; i++) {
            let helpers = {
                _filter: function (item) {
                    return hzs[i].indexOf(item.label) >= 0;
                }
            };

            if (i === 0) {
                parent = linkedArr.filter(helpers._filter)[0];
            } else {
                parent = parent ? parent.children.filter(helpers._filter)[0] : "";
            }
            parent && codeArr.push(parent.code);
        }

        return codeArr;
    }

    /**
     * normalPicer通过code获取汉字数组
     * @param normalArr
     * @param code
     */
    getNormalHZByCode(normalArr, code) {
        if (!code) {
            return "";
        }

        let codes = [];
        if (code.indexOf(",") >= 0) {
            codes = code.split(",");
        } else {
            codes = code.split("/");
        }

        let hzArr = [];
        let parent = {};

        let first = normalArr.first;
        let second = normalArr.second;
        let third = normalArr.third;

        for (let i = 0; i < codes.length; i++) {
            let helpers = {
                _filter: function (item) {
                    return item.code === codes[i];
                }
            };

            if (i === 0 && first) {
                parent = first.filter(helpers._filter)[0];
            }
            if (i === 1 && second) {
                parent = second.filter(helpers._filter)[0];
            }
            if (i === 1 && third) {
                parent = third.filter(helpers._filter)[0];
            }
            parent && hzArr.push(parent.label);
        }
        return hzArr;
    }

    /**
     * normalPicer通过汉字获取code数组
     * @param normalArr
     * @param hz
     */
    getNormalCodeByHZ (normalArr, hz) {
        if (!hz) {
            return "";
        }

        let hzs = [];
        if (hz.indexOf(",") >= 0) {
            hzs = hz.split(",");
        } else {
            hzs = hz.split("/");
        }

        let codeArr = [];
        let parent = {};

        let first = normalArr.first;
        let second = normalArr.second;
        let third = normalArr.third;

        for (let i = 0; i < hzs.length; i++) {
            let helpers = {
                _filter: function (item) {
                    return hzs[i].indexOf(item.label) >= 0;
                }
            };

            if (i === 0 && first) {
                parent = first.filter(helpers._filter)[0];
            }
            if (i === 1 && second) {
                parent = second.filter(helpers._filter)[0];
            }
            if (i === 1 && third) {
                parent = third.filter(helpers._filter)[0];
            }
            parent && codeArr.push(parent.code);
        }
        return codeArr;
    }
}

PickerUtils = new PickerUtils();
module.exports = PickerUtils;